#region Summary

// 
// Owner : JackieHan 
// 
// Solution : LearnSolution 
// 
// Project : JackieHan.NHiberante.SessionManage
// 
// File : NHSessionModule.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 在Http Request时管理NHbernate Session
// 
// History : 
//			2010-09-28 13:23:14	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Web;

#endregion

namespace JackieHan.NHiberante.SessionManage
{
    /// <summary> 
    /// 在Http Request时管理NHbernate Session
    /// </summary>
    public class NHSessionModule:IHttpModule
    {

        public NHSessionModule()
        {
        }


        #region IHttpModule Members

        public void Dispose()
        {
            
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += (obj, e) =>
                                        {
                                            ISessionStorage sessionStorage = SessionStorageFactory.GetSessionStorage();
                                            if (sessionStorage.Get() != null)
                                            {
                                                throw new ApplicationException(
                                                    "there mustn't be current session at the begining of a request");
                                            }
                                        };
            context.EndRequest += (obj, e) =>
                                      {
                                          ISessionStorage sessionStorage = SessionStorageFactory.GetSessionStorage();
                                          if (sessionStorage.Get() != null)
                                          {
                                              sessionStorage.Set(null);
                                          }
                                      };
        }

        #endregion
    }
}